package com.yanqu.road.server.manger.activity.snowgarden.task;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SnowGardenProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.snowgarden.SnowGardenModule;
import com.yanqu.road.server.manger.activity.snowgarden.SnowGardenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.executor.GeneralTask;

public class SnowGardenVisitTask extends GeneralTask {
    private long visitUserId;
    private long visitServerId;
    private int visitType;
    private String visitParam;
    private long targetUserId;
    private long targetServerId;
    private int activityId;
    private long recordTime;

    public SnowGardenVisitTask(int activityId, long visitUserId, long visitServerId, int visitType, String visitParam, long targetUserId, long targetServerId, long recordTime) {
        this.activityId = activityId;
        this.visitUserId = visitUserId;
        this.visitServerId = visitServerId;
        this.visitType = visitType;
        this.visitParam = visitParam;
        this.targetUserId = targetUserId;
        this.targetServerId = targetServerId;
        this.recordTime = recordTime;
    }

    @Override
    public void doRun() {
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(targetUserId);
        int ret = 0;
        if (null != player) {
            ret = player.getModule(SnowGardenModule.class).crossVisit(visitUserId, visitServerId, visitType, visitParam, targetUserId, targetServerId, recordTime);
        } else {
            ret = SnowGardenMgr.crossOffLineVisit(visitUserId, visitServerId, visitType, visitParam, targetUserId, targetServerId, recordTime);
        }
        if (ret > 0) {
            if (ret > 0) {
                SnowGardenProto.SnowGardenToCrossVisitRespMsg.Builder builder = SnowGardenProto.SnowGardenToCrossVisitRespMsg.newBuilder();
                builder.setActivityId(activityId);
                builder.setRet(ret);
                builder.setUserId(visitUserId);
                builder.setUserServerId(visitServerId);
                YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C_SNOW_GARDEN_VISITED_RESP, builder);
                GamePlayerMgr.sendPacket(0, message);
            }
        }
    }
}
